package offer;
/**
 * 约瑟夫:剑指 Offer 62. 圆圈中最后剩下的数字
 */
public class JosephRing {

    public static void main(String[] args) {
        int n = 5, m = 3;
        JosephRing jr = new JosephRing();
        System.out.println(jr.lastRemaining(n, m));
    }

    public int lastRemaining(int n, int m) {
        int x = 0;
        // 像动态规划一样自底向上推
        for (int i = 2; i <= n; i++) {
            x = (x + m) % i;
        }
        return x;
    }
}
